package com.dcloud.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dcloud.enums.BizErrorEnum;
import com.dcloud.exception.BizException;
import com.dcloud.manage.mapper.CollectSourceParamMapper;
import com.dcloud.manage.mapper.CollectSourceParamsDefineMapper;
import com.dcloud.manage.request.CollectSourceAddRequest;
import com.dcloud.manage.request.CollectSourceRequest;
import com.dcloud.manage.service.entity.*;
import com.dcloud.manage.mapper.CollectSourceMapper;
import com.dcloud.manage.service.ICollectSourceService;
import com.dcloud.manage.vo.CollectSourceVO;
import com.dcloud.manage.vo.SourceTestConnVO;
import com.dcloud.utils.ClassUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;

/**
 * @Description: 数据源表
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Service
@Slf4j
public class CollectSourceServiceImpl extends ServiceImpl<CollectSourceMapper, CollectSource> implements ICollectSourceService {

    @Autowired
    CollectSourceMapper tsSourceMapper;

    @Autowired
    CollectSourceParamsDefineMapper collectSourceParamsDefineMapper;

    @Autowired
    CollectSourceParamMapper collectSourceParamMapper;

    @Override
    public DBInfoEntity queryDbInfoBySourceTid(Integer sourceId) {
        DBInfoEntity dbInfoEntity = tsSourceMapper.queryDbInfoBySourceTid(sourceId);
        return dbInfoEntity;
    }

    @Override
    public IPage<CollectSourceVO> queryByPage(CollectSourceRequest collectSourceRequest) {
        Page<CollectSourceVO> pageInfo = new Page<>(collectSourceRequest.getPageNo(),collectSourceRequest.getPageSize());
        IPage<CollectSourceVO> collectSourceIPage = tsSourceMapper.selectJoinPage(pageInfo, CollectSourceVO.class
                , new MPJLambdaWrapper<CollectSource>()
                        .selectAll(CollectSource.class).isNull(CollectSource::getIsBuildIn)
                        .selectAs(CollectSourceDefine::getSourceName, CollectSourceVO::getSourceType)
                        .innerJoin(CollectSourceDefine.class, CollectSourceDefine::getTid, CollectSource::getSourceDefTid)
                        );
        return collectSourceIPage;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveNew(CollectSourceAddRequest collectSourceAddRequest) {
        CollectSource cs = new CollectSource();
        cs.setSourceName(collectSourceAddRequest.getSourceName());
        cs.setSourceDefTid(Integer.valueOf(collectSourceAddRequest.getSourceType()));
        cs.setCreateTime(new Date());

        tsSourceMapper.insert(cs);

        //同时将数据信息配置复制一份空的
        List<CollectSourceParamsDefine> collectSourceParamsDefines = collectSourceParamsDefineMapper.selectList(new LambdaQueryWrapper<CollectSourceParamsDefine>()
                .eq(CollectSourceParamsDefine::getSourceTid, collectSourceAddRequest.getSourceType()));

        collectSourceParamsDefines.forEach(entity -> {
            CollectSourceParam csp = new CollectSourceParam();
            csp.setParamDefTid(entity.getTid());
            csp.setParamValue("");
            csp.setSourceTid(cs.getTid());
            csp.setCreateTime(new Date());
            collectSourceParamMapper.insert(csp);
        });

    }

    /**
     * @Description: 根据传参查询数据源
     * @param collectSourceRequest
     * @return: List<CollectSourceVO>
     * @Author: shijf
     * @Date 2024/3/13 15:16
     */
    @Override
    public List<CollectSourceVO> queryListByReq(CollectSourceRequest collectSourceRequest) {
        List<CollectSourceVO> collectSourceVOS = tsSourceMapper.queryListByReq(collectSourceRequest);
        return collectSourceVOS;
    }

    @Override
    public CollectSourceVO queryInfoById(String id) {
        CollectSourceVO vo = tsSourceMapper.queryInfoById(id);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        CollectSource collectSource = tsSourceMapper.selectOne(new LambdaQueryWrapper<CollectSource>().eq(CollectSource::getTid, id));
        if(collectSource.getIsBuildIn() == 1){
            throw new BizException(BizErrorEnum.METADATA_ERROR_INNERSOURE_DELETE);
        }

        collectSourceParamMapper.delete(new LambdaQueryWrapper<CollectSourceParam>().eq(CollectSourceParam::getSourceTid,collectSource.getTid()));
        tsSourceMapper.deleteById(id);
    }
}
